package top.chenfu.blog.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.chenfu.blog.beans.ResponseData;
import top.chenfu.blog.entity.Article;
import top.chenfu.blog.service.ArticleService;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 超級管理員專屬
 *
 * @Auther chenfu
 * @2019/10/30 16:52
 * @Desc
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private ArticleService articleService;

    /**
     * @param page
     * @param count
     * @param keywords
     * @return
     */
    @GetMapping("/article/all")
    public Map<String, Object> getArticleByStateByAdmin(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "count", defaultValue = "6") Integer count, String keywords) {
        List<Article> articles = articleService.getArticleByState(-2, null, page, count, keywords);
        Map<String, Object> map = new HashMap<>();
        map.put("articles", articles);
        map.put("totalCount", articleService.getArticleCountByState(1, null, keywords));
        return map;
    }

    /**
     * 回收站和删除
     *
     * @param aids
     * @param status
     * @return
     */
    @PutMapping("/article/dustbin")
    public ResponseData updateArticleState(Long[] aids, Integer status) {
        List<Long> ids = Arrays.asList(aids);
        if (articleService.updateArticleState(ids, status) == aids.length) {
            ResponseData success = ResponseData.defaultSuccess();
            success.setMsg("删除成功");
            return success;
        }
        ResponseData fail = ResponseData.defaultFail();
        fail.setMsg("删除失败");
        return fail;
    }

}
